package com.bs.visitservice.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.entity.Product;
import com.bs.visitservice.entity.ProductType;
import com.bs.visitservice.service.ProductService;
import com.bs.visitservice.service.ProductTypeService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品信息管理
 */
@RestController
@RequestMapping("/userpublic")
public class UserProductController {

    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ProductService productService;


    /**
     * 查询店铺产品类型
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryProductType",method = RequestMethod.POST)
    public Result queryProductType(@RequestBody Map cond){
        ProductType pt = (ProductType)ResultUtil.transObject(cond, ProductType.class);
        LambdaQueryWrapper<ProductType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductType::getSellerNo,pt.getSellerNo());
        List<Map<String, Object>> maps = productTypeService.listMaps(wrapper);
        return  Result.success(maps);
    }
    /**
     * 按照条件查询店铺产品
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryProduct",method = RequestMethod.POST)
    public Result queryProduct(@RequestBody Map cond){
        Product pt = (Product)ResultUtil.transObject(cond, Product.class);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        //用户只能查看上架的产品
        wrapper.eq(Product::getProductType,pt.getProductType()).eq(Product::getSellerNo,pt.getSellerNo()).eq(Product::getStatus,0);
        IPage page = ParamUtils.getPage(cond);
        page = productService.pageMaps(page,wrapper);
        List<Map<String, Object>> maps = page.getRecords();
        Map<String,Object> result = new HashMap<>();
        result.put("total",page.getTotal());
        result.put("datas",maps);
        return  Result.success(result);
    }


}
